package com.webinsky.persist.dao.resource;

import java.util.List;

import com.webinsky.persist.entity.resource.Video;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.persist.entity.resource.Image;
import com.webinsky.persist.entity.resource.ImageFilter;

public interface ImageMapper {
    @Insert({
        "insert into image (name, site_id, file_name, del, create_at, update_at, language)",
        "values (#{name,jdbcType=VARCHAR}, #{siteId, jdbcType=INTEGER}, #{file_name,jdbcType=VARCHAR}, ",
        "#{del,jdbcType=INTEGER}, now(), now(), #{language, jdbcType=VARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(Image record);

    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectAll();
    
    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectAcitveBySiteId(@Param("siteId")int siteId, @Param("del")int del);

    @SelectProvider(type = ImageProvider.class, method = "selectByIds")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
            @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
            @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
            @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectByIds(@Param("ids")List<Integer> ids);
    
    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image where language = #{lang, jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectByLang(@Param("lang")String lang);
    
    @SelectProvider(type = ImageProvider.class, method = "selectByPagnation")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectByPagnation(@Param("filter")ImageFilter filter);
    
    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image where site_id = #{siteId, jdbcType=INTEGER} and name = #{name, jdbcType=VARCHAR} and language = #{lang, jdbcType=VARCHAR} and del = #{del, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectBySiteIdNameAndLangDel(@Param("siteId")int siteId, @Param("name")String name, @Param("lang")String lang, @Param("del")int del);    
    
    
    @SelectProvider(type = ImageProvider.class, method = "selectCountByPagnation")
    int selectCountByPagnation(@Param("filter")ImageFilter filter);
    
    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image where id = #{id, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    Image selectByPrimaryKey(@Param("id")int id);
    
    @Select({
        "select",
        "id, name, site_id, file_name, del, create_at, update_at, language",
        "from image where name like CONCAT('%',#{name},'%') and language = #{lang, jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        @Result(column="file_name", property="file_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
    })
    List<Image> selectByNameAndLang(@Param("name")String name, @Param("lang")String lang);
    
    @Select({
        "select count(*) from image",
        "where language = #{lang, jdbcType=VARCHAR} and del = 0"
    })
    int selectCountByLang(@Param("lang")String lang);
    
    @Update({
        "update image",
        "set name = #{name,jdbcType=VARCHAR},",
        "site_id = #{siteId, jdbcType=INTEGER},",
        "file_name = #{file_name,jdbcType=VARCHAR},",
        "del = #{del,jdbcType=INTEGER},",
        "create_at = #{create_at,jdbcType=TIMESTAMP},",
        "update_at = #{update_at,jdbcType=TIMESTAMP},",
        "language = #{language,jdbcType=VARCHAR}",
        "where Id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Image record);    
    
}